﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace UtilZ.Dotnet.Ex.TTLV
{
    /// <summary>
    /// TTLV编解码类
    /// </summary>
    public class TTLVNodeDefaultCodec : ITTLVNodeCodec
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public TTLVNodeDefaultCodec()
        {

        }

        /// <summary>
        /// 顺序编码节点数据
        /// </summary>
        /// <param name="bw">BinaryWriter</param>
        /// <param name="tag">Tag</param>
        /// <param name="typeCode">Type</param>
        /// <param name="valueBuffer">Value</param>
        public void WriteNode(BinaryWriter bw, int tag, TypeCode typeCode, byte[] valueBuffer)
        {
            bw.Write(BitConverter.GetBytes(tag));
            bw.Write((byte)typeCode);
            bw.Write(BitConverter.GetBytes(valueBuffer.Length));
            bw.Write(valueBuffer);
        }

        /// <summary>
        /// 读取节点信息
        /// </summary>
        /// <param name="br">BinaryReader</param>
        /// <param name="nodeInfo">存储节点信息对象</param>
        public void ReadNodeInfo(BinaryReader br, TTLVNodeInfo nodeInfo)
        {
            nodeInfo.Tag = br.ReadInt32();
            nodeInfo.Type = (TypeCode)br.ReadByte();
            nodeInfo.Length = br.ReadInt32();
        }
    }
}
